#!/usr/bin/env python
# -*- coding = utf-8 -*-
# @Time      : 2021/1/16 22:22
# @Author    : Reanon
# @File      : test
# @Email     : dafo360@gmail.com
# @Software  : PyCharm

import json

from solidity_parser import parser

"""
将智能合约生成抽象语法树.ast 文件格式
"""

# 定义合约名称
contract_name = "SimpleStore"
# 解析合约生成抽象语法树
sourceUnit = parser.parse_file("../contracts/" + contract_name + ".sol")
# 打印生成的AST
# pprint.pprint(sourceUnit)

# 将数据转换为字典格式
sourceUnitObject = parser.objectify(sourceUnit)

# 将字典转换为JSON格式的字符串，并将转化后的结果写入文件
filename = "./" + contract_name + "_ast.json"
with open(filename, 'w', encoding='UTF-8') as file:
    # indent=4 缩进为4
    file.write(json.dumps(sourceUnit, indent=4, ensure_ascii=False))
